// 3.Compiler解析指令：解析每个元素的指令/插值表达式，并转换为data中的数据
class Compiler {
    constructor(vm) {
        this.vm = vm
        this.el = vm.$el

        this.compile(this.el)
    }

    compile(el) {
        const nodes = el.childNodes
        Array.from(nodes).forEach(node => {
            if(this.isTextNode(node)) {
                this.compileText(node)
            } else if(this.isElementNode) {
                this.compileElement(node)
            }

            if(node.childNodes && node.childNodes.length) {
                this.compile(node)
            }
        })
    }

    isTextNode(node) {
        return node.nodeType === 3
    }

    isElementNode(node) {
        return node.nodeType === 1
    }

    isDirective(attrName) {
        return attrName.startsWith('v-')
    }

    // 是否为v-on指令
    isEventDirective(attrName) {
        return attrName.startsWith('on:')
    }

    compileText(node) {
        const reg = /\{\{(.+)\}\}/
        const value = node.textContent
        if (reg.test(value)) { 
            // 插值表达式中的值就是我们要的属性名称 
            let key = RegExp.$1.trim() 
            // 把插值表达式替换成具体的值
            if(key.indexOf('.') > -1) {
                key = key.split('.') // ['person', 'name']
                let val = this.vm
                key.forEach(item => {
                    val = val[item] // {name: '12'}
                })
                node.textContent = value.replace(reg, val)
            } else {
                node.textContent = value.replace(reg, this.vm[key])
            }
            // 创建watcher实例
            new Watcher(this.vm, key, newValue => {
                node.textContent = newValue
            })
        }
    }

    compileElement(node) {
        Array.from(node.attributes).forEach(attr => {
            let attrName = attr.name // v-text
            // 判断属性是否为指令
            if(this.isDirective(attrName)) {
                attrName = attrName.slice(2) // text/model/html/on:click
                const key = attr.value // v-text="key"
                // 判断是否为v-on指令
                if(this.isEventDirective(attrName)) {
                    attrName = attrName.slice(3) // 'click'
                    this.onUpdater(node, key, attrName)
                } else {
                    this.updater(node, key, attrName)
                }
            }
        })
    }

    updater(node, key, dir) {
        const updaterFn = this[dir + 'Updater']
        updaterFn && updaterFn.call(this, node, this.vm[key], key)
    }

    // v-text
    textUpdater(node, value, key) {
        node.textContent = value

        new Watcher(this.vm, key, newValue => {
            node.textContent = newValue
        })
    }

    // v-model
    modelUpdater(node, value, key) {
        node.value = value

        new Watcher(this.vm, key, newValue => {
            node.value = newValue
        })
        // 双向绑定
        node.addEventListener('input', () => {
            this.vm[key] = node.value
        })
    }

    // v-html
    htmlUpdater(node, value, key) {
        node.innerHTML = value

        new Watcher(this.vm, key, newValue => {
            node.innerHTML = newValue
        })
    }

    // v-on
    onUpdater(node, value, key) {
        // value是字符串时，在vm的methods中寻找匹配的函数，否则直接调用value
        let functionHandle = typeof value === 'string' ? this.vm.methods[value] : value
        node.addEventListener(key, () => {
            functionHandle()
        })
    }
}